package lm.concurrency.day6;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.SynchronousQueue;

import static lm.concurrency.util.Sleeper.sleep;

/**
 * @Auther: Min.Liu
 * @Description: SynchronousQueue是 newCachedThreadPool 带缓冲的线程池使用的等待队列
 * @Date: Created in 14:38 2024/11/8
 */
@Slf4j
public class SynchronousQueueTest {
    public static void main(String[] args) {
        //里面的东西 有人要取 才能存进去 一手交钱 一手交货
        SynchronousQueue<Integer> integers = new SynchronousQueue<>();
        new Thread(() -> {
            try {
                log.debug("putting {} ", 1);
                integers.put(1);
                log.debug("{} putted...", 1);
                log.debug("putting...{} ", 2);
                integers.put(2);
                log.debug("{} putted...", 2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        sleep(1);
        new Thread(() -> {
            try {
                log.debug("taking {}", 1);
                integers.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();
        sleep(1);
        new Thread(() -> {
            try {
                log.debug("taking {}", 2);
                integers.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t3").start();
    }
}
